<?php


namespace app\im\liveChat;


use app\im\BaseController;
class chatController extends BaseController
{
    public function join($event, $data)
    {
        if (empty($data['room'])) throw new \Exception('请选择需要退出的房间', 401);


//        $this->websocket -> room -> jo

        $join = app(group::class, [
            $this->websocket
        ]) -> join($data['room']);

        if ($join) {
            return $this->websocket -> emit($event, [
                'code' => 200,
                'msg' => '加入成功'
            ]);
        }

        return $this->websocket -> emit($event, [
            'code' => 201,
            'msg' => '加入失败请重试'
        ]);
    }

    public function leave($event, $data)
    {
        if (empty($data['room'])) throw new \Exception('请选择需要退出的房间', 401);
        $join = app(group::class, [
            $this->websocket
        ]) -> leave($data['room']);
        if ($join) {
            return $this->websocket -> emit($event, [
                'code' => 200,
                'msg' => '退出成功'
            ]);
        }
        return $this->websocket -> emit($event, [
            'code' => 201,
            'msg' => '退出失败请重试'
        ]);
    }

    // 发送信息至好友
    public function sendToFriend(string $event, mixed $data) {

    }

    // 发送信息至群组
    public function sendToGroup(string $room_name, string $event, mixed $data)
    {
        if (empty($data['room'])) throw new \Exception('请选择需要退出的房间', 401);
        app(group::class, [
            $this->websocket
        ]) -> emit($room_name, $event, $data);
    }
}